#include "StdAfx.h"
#include "SupportImage.h"

#define KEYNAME_IMAGE_PATH		_T("Path")
#define KEYNAME_IMAGE_ID		_T("ID")
#define KEYNAME_IMAGE_POS_X		_T("PosX")
#define KEYNAME_IMAGE_POS_Y		_T("PosY")

#define KEYNAME_WND_LEFT				_T("left")
#define KEYNAME_WND_RIGHT				_T("right")
#define KEYNAME_WND_TOP					_T("top")
#define KEYNAME_WND_BOTTOM				_T("bottom")
#define KEYNAME_WND_DIV_LEFT			_T("div_left")
#define KEYNAME_WND_DIV_RIGHT			_T("div_right")
#define KEYNAME_WND_DIV_TOP				_T("div_top")
#define KEYNAME_WND_DIV_BOTTOM			_T("div_bottom")
#define KEYNAME_WND_STICK_TO_LEFT		_T("StickToLeft")
#define KEYNAME_WND_STICK_TO_RIGHT		_T("StickToRight")
#define KEYNAME_WND_STICK_TO_TOP		_T("StickToTop")
#define KEYNAME_WND_STICK_TO_BOTTOM		_T("StickToBottom")
#define KEYNAME_WND_STICK_TO_HCENTER	_T("StickToHCenter")
#define KEYNAME_WND_STICK_TO_VCENTER	_T("StickToVCenter")
#define KEYNAME_WND_MODE				_T("Mode")
#define KEYNAME_WND_PARENT				_T("Parent")
#define KEYNAME_WND_COLOR_BG			_T("Color BG")
#define KEYNAME_WND_COLOR_TEXT			_T("Color Text")
#define KEYNAME_WND_COLOR				_T("Color %02d")




CSupportImage::CSupportImage(void)
{
}

CSupportImage::~CSupportImage(void)
{
}

BOOL CSupportImage::SetImageRootPath(CString strImageRootPath, CString strImageInfoFileName)
{
	CString strProfilePath;
	strProfilePath.Format(_T("%s\\%s"), strImageRootPath, strImageInfoFileName);

	TRACE(_T("%s\n"), strProfilePath);

	CFile cFile;
	BOOL bRet;
	bRet = cFile.Open(strProfilePath, CFile::modeRead);
	if (bRet)
	{
		cFile.Close();

		m_strImageRootPath = strImageRootPath;
		m_strImageInfoFileName = strImageInfoFileName;
		m_strProfilePath.Format(_T("%s\\%s"), m_strImageRootPath, m_strImageInfoFileName);

	}
	return bRet;
}

BOOL CSupportImage::CreateBmpInfo(CString strImageName, LPST_BMP_INFO lpBmpInfo)
{
	TCHAR tzPath[MAX_PATH];
	TCHAR tzFullPath[MAX_PATH];
	DWORD dwRet;

	ZeroMemory(lpBmpInfo, sizeof(ST_BMP_INFO));
	ZeroMemory(tzPath, MAX_PATH*sizeof(TCHAR));
	dwRet = GetPrivateProfileString(strImageName, KEYNAME_IMAGE_PATH, NULL, tzPath, MAX_PATH, m_strProfilePath);

	if (dwRet==0)
		return FALSE;
	lpBmpInfo->uImageID = GetPrivateProfileInt(strImageName, KEYNAME_IMAGE_ID, -1, m_strProfilePath);
	if (lpBmpInfo->uImageID == (UINT)-1)
		return FALSE;

	wsprintf(tzFullPath, _T("%s\\%s"), m_strImageRootPath, tzPath);

	lpBmpInfo->hbmpImage = (HBITMAP)LoadImage(GetModuleHandle(NULL), tzFullPath, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE|LR_LOADFROMFILE);
	if (lpBmpInfo->hbmpImage == NULL)
		return FALSE;

	BITMAP bm;
	GetObject(lpBmpInfo->hbmpImage, sizeof(bm), &bm);

	lpBmpInfo->rtImageRect.left = 0;
	lpBmpInfo->rtImageRect.right = bm.bmWidth;
	lpBmpInfo->rtImageRect.top = 0;
	lpBmpInfo->rtImageRect.bottom = bm.bmHeight;

	lpBmpInfo->ptImage.x = GetPrivateProfileInt(strImageName, KEYNAME_IMAGE_POS_X, 0, m_strProfilePath);
	lpBmpInfo->ptImage.y = GetPrivateProfileInt(strImageName, KEYNAME_IMAGE_POS_Y, 0, m_strProfilePath);

	return TRUE;
}

void CSupportImage::ReleaseBmpInfo(LPST_BMP_INFO lpBmpInfo)
{
	DeleteObject(lpBmpInfo->hbmpImage);
}

BOOL CSupportImage::GetWndInfo(CString strWndName, LPST_WND_POS lpWndPos)
{
	if (!GetWndRect(strWndName, &lpWndPos->rtWndPos))
		return FALSE;
	
	DWORD dwRet;
	dwRet = GetPrivateProfileString(strWndName, KEYNAME_WND_PARENT, NULL, lpWndPos->tzParentWnd, MAX_PATH, m_strProfilePath);
	if (dwRet!=0)
	{
		GetWndRect(lpWndPos->tzParentWnd, &lpWndPos->rtParentPos);
	}

	lpWndPos->rtDiv.left = GetPrivateProfileInt(strWndName, KEYNAME_WND_DIV_LEFT, 0, m_strProfilePath);
	lpWndPos->rtDiv.right = GetPrivateProfileInt(strWndName, KEYNAME_WND_DIV_RIGHT, 0, m_strProfilePath);
	lpWndPos->rtDiv.top = GetPrivateProfileInt(strWndName, KEYNAME_WND_DIV_TOP, 0, m_strProfilePath);
	lpWndPos->rtDiv.bottom = GetPrivateProfileInt(strWndName, KEYNAME_WND_DIV_BOTTOM, 0, m_strProfilePath);

	lpWndPos->rtStickTo.left = GetPrivateProfileInt(strWndName, KEYNAME_WND_STICK_TO_LEFT, -1, m_strProfilePath);
	lpWndPos->rtStickTo.right = GetPrivateProfileInt(strWndName, KEYNAME_WND_STICK_TO_RIGHT, -1, m_strProfilePath);
	lpWndPos->rtStickTo.top = GetPrivateProfileInt(strWndName, KEYNAME_WND_STICK_TO_TOP, -1, m_strProfilePath);
	lpWndPos->rtStickTo.bottom = GetPrivateProfileInt(strWndName, KEYNAME_WND_STICK_TO_BOTTOM, -1, m_strProfilePath);

	lpWndPos->bStickToHCenter = GetPrivateProfileInt(strWndName, KEYNAME_WND_STICK_TO_HCENTER, 0, m_strProfilePath);
	lpWndPos->bStickToVCenter = GetPrivateProfileInt(strWndName, KEYNAME_WND_STICK_TO_VCENTER, 0, m_strProfilePath);
	lpWndPos->nMode = GetPrivateProfileInt(strWndName, KEYNAME_WND_MODE, 0, m_strProfilePath);

	CString strKeyName;
	int i;
	for (i=0;i<14;i++)
	{
		strKeyName.Format(KEYNAME_WND_COLOR, i);
		lpWndPos->dwColorLevel[i] = GetPrivateProfileInt(strWndName, strKeyName, RGB(0,0,0), m_strProfilePath);
	}
	lpWndPos->dwColorBackground = GetPrivateProfileInt(strWndName, KEYNAME_WND_COLOR_BG, RGB(0,0,0), m_strProfilePath);
	lpWndPos->dwColorText = GetPrivateProfileInt(strWndName, KEYNAME_WND_COLOR_TEXT, RGB(0,0,0), m_strProfilePath);
	return TRUE;
}

BOOL CSupportImage::GetWndRect(CString strWndName, CRect *rtPos)
{
	rtPos->left = GetPrivateProfileInt(strWndName, KEYNAME_WND_LEFT, -1, m_strProfilePath);
	rtPos->right = GetPrivateProfileInt(strWndName, KEYNAME_WND_RIGHT, -1, m_strProfilePath);
	rtPos->top = GetPrivateProfileInt(strWndName, KEYNAME_WND_TOP, -1, m_strProfilePath);
	rtPos->bottom = GetPrivateProfileInt(strWndName, KEYNAME_WND_BOTTOM, -1, m_strProfilePath);
	return TRUE;

}